import { BaseFilter } from './base';

// 权限类型
export type PermissionType = 'menu' | 'button' | 'api' | 'data';

// 权限实体
export interface Permission {
  id: string;
  code: string;
  name: string;
  description?: string;
  type: PermissionType;
  module: string;
  action: string;
  resource: string;
  parentId?: string;
  level: number;
  path: string;
  sort: number;
  isSystem: boolean;
  createdAt: string;
  updatedAt: string;
  // 计算属性
  children?: Permission[];
  hasChildren?: boolean;
}

// 角色实体
export interface Role {
  id: string;
  tenantId: string;
  organizationId?: string;
  name: string;
  code: string;
  description?: string;
  permissions: string[];
  isSystem: boolean;
  isDefault: boolean;
  status: 'active' | 'inactive';
  sort: number;
  createdAt: string;
  updatedAt: string;
  createdBy?: string;
  updatedBy?: string;
  // 计算属性
  userCount?: number;
  permissionList?: Permission[];
}

// 角色查询过滤器
export interface RoleFilter extends BaseFilter {
  tenantId?: string;
  organizationId?: string;
  isSystem?: boolean;
  isDefault?: boolean;
}

// 权限查询过滤器
export interface PermissionFilter extends BaseFilter {
  type?: PermissionType;
  module?: string;
  parentId?: string;
  isSystem?: boolean;
}

// 角色表单数据
export interface RoleFormData {
  name: string;
  code: string;
  description?: string;
  permissions: string[];
  status: 'active' | 'inactive';
  organizationId?: string;
  sort?: number;
}

// 创建角色请求
export interface CreateRoleRequest extends RoleFormData {
  tenantId: string;
}

// 更新角色请求
export interface UpdateRoleRequest extends Partial<RoleFormData> {}

// 权限矩阵项
export interface PermissionMatrixItem {
  permissionId: string;
  permissionName: string;
  permissionCode: string;
  module: string;
  granted: boolean;
  inherited?: boolean;
  source?: string; // 权限来源（继承自哪个角色）
}

// 权限分配请求
export interface AssignPermissionsRequest {
  roleId: string;
  permissions: string[];
}

// 权限树节点
export interface PermissionTreeNode {
  id: string;
  name: string;
  code: string;
  type: PermissionType;
  module: string;
  parentId?: string;
  level: number;
  sort: number;
  children: PermissionTreeNode[];
  checked?: boolean;
  indeterminate?: boolean;
  expanded?: boolean;
}

// 数据权限规则
export interface DataPermissionRule {
  id: string;
  name: string;
  code: string;
  description?: string;
  scope: 'all' | 'organization' | 'department' | 'self' | 'custom';
  condition?: string; // JSON字符串，自定义条件
  isDefault: boolean;
  sort: number;
}

// 用户权限上下文
export interface UserPermissionContext {
  userId: string;
  tenantId: string;
  organizationId: string;
  roles: Role[];
  permissions: Permission[];
  dataScope: DataPermissionRule;
  customDataScope?: any;
}